import matplotlib.pyplot as plt
import torch
from scipy import signal
import numpy as np
import torch.nn as nn


fs = 200.0  # 采样频率 (Hz)
t = np.linspace(0, 120.0, 120*int(fs), endpoint=False)  # 1秒钟的时间向量(1s 200个)
# x = np.sin(2 * np.pi * 10 * t) +  np.sin(2 * np.pi * 50 * t)  # 合成信号
d1 = np.loadtxt('D:/multimodal_health/dataset/Dataset EMG Fatigue/Data as txt Files/sub1.txt')
data = d1[10:24010,1]

# 低通滤波器
b1, a1 = signal.butter(8, 0.07, 'lowpass')  # 配置滤波器 8 表示滤波器的阶数
# 截止频率为140Hz，采样频率为4000Hz，Wn=2*140/4000 = 0.07
filtedData1 = signal.filtfilt(b1, a1, data)  # data为要过滤的信号

plt.subplot(2,1,1)
plt.title("Original Signal")
plt.plot(t,data,color = 'orange',label = 'Original signal')
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.grid()

# 高通滤波器
b2, a2 = signal.butter(8, 0.0075, 'highpass')
# 截止频率为15Hz，采样频率为4000Hz，Wn=2*15/4000 = 0.0075
filtedData2 = signal.filtfilt(b2, a2, filtedData1)

plt.subplot(2,1,2)
plt.title("Filtered Signal")
plt.plot(t,filtedData2,color = 'orange',label = 'high pass filtered signal')
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.grid()

plt.tight_layout()
plt.show()

# 将数据转换成240 * 100，一行是500ms的数据
x = filtedData2.reshape(1,-1)
x = x.reshape(240,100)
x_copy = x.copy()
y = torch.from_numpy(x_copy)
y = y.unsqueeze(1)
print(y.shape)
